package com.hongtong.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hongtong.auto.JSONAuthentication;
import com.hongtong.entity.TeAn;
import com.hongtong.entity.TeCur;
import com.hongtong.entity.TeQa;
import com.hongtong.mapper.TeAnMapper;
import com.hongtong.mapper.TeTaskMapper;
import com.hongtong.mapper.TeUserMapper;
import com.hongtong.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hongtong
 * @since 2022-04-13
 */
@RestController
@RequestMapping("/te-an")
public class TeAnController extends JSONAuthentication {
    @Autowired
    TeAnMapper teAnMapper;
    @Autowired
    TeTaskMapper teTaskMapper;
    @Autowired
    TeUserMapper teUserMapper;

    @ApiOperation(value = "添加作业答案(分数不用给，默认为0)")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody TeAn teAn) throws  Exception{
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.eq("ta_id",teAn.getTaId());
            wrapper.eq("user_id",teAn.getUserId());
            if(teAnMapper.selectOne(wrapper)!=null){
                Result error = Result.error().message("已回答此作业答案,不可重复回答");
                this.WriteJSON(request, response, error);
            }else{
                if(teAnMapper.insert(teAn)>0){
                    Result ok = Result.ok();
                    this.WriteJSON(request, response, ok);
                }else{
                    Result error = Result.error();
                    this.WriteJSON(request, response, error);
                }
            }

        }

    @ApiOperation(value = "删除作业答案")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=teAnMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改答案内容")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestParam("value")String value,@RequestParam("id") int id) throws  Exception{
        TeAn teAn = teAnMapper.selectById(id);
        teAn.setValue(value);
        if(teAnMapper.updateById(teAn)>0){
            Result ok = Result.ok().message("修改成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "给答案分")
    @PostMapping("/updGf")
    public void updGf(HttpServletRequest request, HttpServletResponse response, @RequestParam("fraction")int fraction,@RequestParam("id") int id) throws  Exception{
        TeAn teAn = teAnMapper.selectById(id);
        teAn.setFraction(fraction);
        if(teAnMapper.updateById(teAn)>0){
            Result ok = Result.ok().message("修改成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有作业答案")
    @PostMapping("/findAll")
    public void findAll(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<TeAn> shUserPage=new Page<>(current,size);
        Page<TeAn> shUserPages=teAnMapper.selectPage(shUserPage,null);
        List<TeAn> records = shUserPages.getRecords();
        for (TeAn a:records){
            a.setTeUser(teUserMapper.selectById(a.getUserId()));
            a.setTeTask(teTaskMapper.selectById(a.getTaId()));
        }
        int total=teAnMapper.selectCount(null);
        if(records.size()>0){
            Result ok = Result.ok().data("test",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找作业答案根据学生id")
    @PostMapping("/findAllByUserName")
    public void findAllByUserName(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("userId")int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<TeAn> shUserPage=new Page<>(current,size);
        Page<TeAn> shUserPages=teAnMapper.selectPage(shUserPage,wrapper);
        List<TeAn> records = shUserPages.getRecords();
        for (TeAn a:records){
            a.setTeUser(teUserMapper.selectById(a.getUserId()));
            a.setTeTask(teTaskMapper.selectById(a.getTaId()));
        }
        int total=teAnMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("test",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找作业答案根据作业id")
    @PostMapping("/findAllByUserTeId")
    public void findAllByUserTeId(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("taId")int taId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("ta_id",taId);
        Page<TeAn> shUserPage=new Page<>(current,size);
        Page<TeAn> shUserPages=teAnMapper.selectPage(shUserPage,wrapper);
        List<TeAn> records = shUserPages.getRecords();
        for (TeAn a:records){
            a.setTeUser(teUserMapper.selectById(a.getUserId()));
            a.setTeTask(teTaskMapper.selectById(a.getTaId()));
        }
        int total=teAnMapper.selectCount(wrapper);
        if(records.size()>0){
            Result ok = Result.ok().data("test",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查找作业答案根据id")
    @PostMapping("/findAllById")
    public void findAllById(HttpServletRequest request, HttpServletResponse response,@RequestParam("id") int id)throws  Exception{
        TeAn teAn=teAnMapper.selectById(id);
        teAn.setTeUser(teUserMapper.selectById(teAn.getUserId()));
        teAn.setTeTask(teTaskMapper.selectById(teAn.getTaId()));
        if(teAn!=null){
            Result ok = Result.ok().data("test",teAn);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "根据用户id和作业id查找答案")
    @PostMapping("/findAllByUserIdTaskId")
    public void findAllByUserIdTaskId(HttpServletRequest request, HttpServletResponse response,@RequestParam("taId") int taId,@RequestParam("userId")int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("ta_id",taId);
        wrapper.eq("user_id",userId);
        TeAn teAn = teAnMapper.selectOne(wrapper);
        if(teAn!=null){
            Result ok = Result.ok().data("tean",teAn);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

